package com.kaifamiao.www.Thread.do0927;

import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CachedThreadPoolDemo {
    public static void main(String[] args) {
        /**
         * CachedThreadPool:线程数量不定的线程池，最大线程数为Integer.MAX_VALUE,线程池长度超过处理需要，可灵活回收空闲线程，
         * 若无可回收，则新建线程。空闲线程有超时限制，超过60s限制线程被终止并被删除。
         */
        ExecutorService pool = Executors.newCachedThreadPool();
        System.out.println("当前时间" + LocalTime.now());
        for (int i = 0;i<10;i++){
            int number = i;
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前时间"+LocalTime.now()+",线程："+Thread.currentThread().getName()+",序号:"+number);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}
